﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Threading;
using System.Diagnostics;

namespace GTA.DC.Common.UC
{
    /// <summary>
    /// 状态提示窗体
    /// </summary>
    public partial class WaitingForm : Form
    {
        private static WaitingForm wf;
        private static bool isShow = false;
        private static string message;

        #region 属性
        /// <summary>
        /// 显示的状态信息
        /// </summary>
        public string Message
        {
            get
            {
                return lblStatus.Text;
            }

            set
            {
                lblStatus.Text = value;
            }
        }
        #endregion

        #region 构造函数
        public WaitingForm()
        {
            InitializeComponent();
        }
        #endregion

        #region 私有函数
        private void WaitingForm_Load(object sender, EventArgs e)
        {
            this.Message = WaitingForm.message;
            Debug.WriteLine("Load");
        }

        private void WaitingForm_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            base.Close();
        }

        private void Close2(object sender, EventArgs e)
        {
            Debug.WriteLine("Close2");
            base.Close();
        }

        private void ShowMessage(object sender, EventArgs e)
        {
            if (isShow)
            {
                Debug.WriteLine("ShowMessage");
                this.Message = WaitingForm.message;
                this.Show();
            }
        }

        //protected override void OnShown(EventArgs e)
        //{
        //    base.OnShown(e);
        //    //if (!isShow)
        //    //{
        //    //    Debug.WriteLine("OnShown Close");
        //    //    base.Close();
        //    //}
        //}
        #endregion

        #region 窗体拖动2
        [DllImport("user32.dll", EntryPoint = "SendMessage")]
        public static extern int SendMessage(int hWnd, int wMsg, int wParam, int lParam);
        [DllImport("user32.dll", EntryPoint = "ReleaseCapture")]
        public static extern int ReleaseCapture();
        public const int WM_SysCommand = 0x0112;
        public const int SC_MOVE = 0xF012;

        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);
            ReleaseCapture();
            SendMessage(this.Handle.ToInt32(), WM_SysCommand, SC_MOVE, 0);
        }
        #endregion

        #region 无焦点窗体
        [System.Runtime.InteropServices.DllImport("user32.dll")]
        private extern static IntPtr SetActiveWindow(IntPtr handle);

        private const int WM_ACTIVATE = 0x006;
        private const int WM_ACTIVATEAPP = 0x01C;
        private const int WM_NCACTIVATE = 0x086;
        private const int WA_INACTIVE = 0;
        private const int WM_MOUSEACTIVATE = 0x21;
        private const int MA_NOACTIVATE = 3;

        protected override void WndProc(ref Message m)
        {
            if (m.Msg == WM_MOUSEACTIVATE)
            {
                m.Result = new IntPtr(MA_NOACTIVATE);
                return;
            }
            else if (m.Msg == WM_NCACTIVATE)
            {
                if (((int)m.WParam & 0xFFFF) != WA_INACTIVE)
                {
                    if (m.LParam != IntPtr.Zero)
                    {
                        SetActiveWindow(m.LParam);
                    }
                    else
                    {
                        SetActiveWindow(IntPtr.Zero);
                    }
                }
            }
            base.WndProc(ref m);
        }
        #endregion

        #region 静态方法

        /// <summary>
        /// 显示状态提示窗体
        /// </summary>
        /// <param name="message">提示的信息</param>
        public static void Show(string message)
        {
            try
            {
                isShow = true;
                WaitingForm.message = message;
                if (wf == null || wf != null && wf.IsDisposed)
                {
                    wf = new WaitingForm();
                    Thread thread = new Thread(new ThreadStart(RunForm));
                    thread.Start();
                }
                else
                {
                    if (wf.Created)
                    {
                        wf.Invoke(new EventHandler(wf.ShowMessage), new object[] { });
                    }
                }
            }
            catch
            {
            }
        }

        /// <summary>
        /// 关闭状态提示窗体
        /// </summary>
        public new static void Close()
        {
            try
            {
                isShow = false;
                Debug.WriteLine("Close");
                if (wf != null)
                {
                    if (wf.Created)
                    {
                        Debug.WriteLine("wf.Invoke(new EventHandler(wf.Close2)");
                        wf.Invoke(new EventHandler(wf.Close2), new object[] { });
                    }
                    //wf = null;
                }
            }
            catch
            {
            }
        }

        private static void RunForm()
        {
            if (wf != null && isShow)
            {
                Debug.WriteLine("Application.Run");
                Application.Run(wf);
            }
        }

        #endregion
    }
}
